package com.example.administrator.fangbook.activity.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.fangbook.R;
import com.example.administrator.fangbook.activity.MainActivity;
import com.example.administrator.fangbook.dataLibrary.book.ShelfItemBook;
import com.example.administrator.fangbook.importlocal.ImportLocalBookActivity;
import com.example.administrator.fangbook.utils.ToastUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainAdapter extends RecyclerView.Adapter {
    private final MainActivity mContext;
    private List<ShelfItemBook> mDatas;
    private final LayoutInflater inflater;
    private OnItemClickLisener mOnItemClickLisener;

    public MainAdapter(MainActivity mainActivity) {
        this.mContext = mainActivity;
        inflater = LayoutInflater.from(mainActivity);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mDatas.size()) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == 1) {
            viewHolder = new FootViewHolder(inflater.inflate(R.layout.main_content_item, parent, false));
        } else {
            viewHolder = new ContentViewHolder(inflater.inflate(R.layout.main_foot_item, parent, false));
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentViewHolder) {
            final ShelfItemBook shelfItemBook = mDatas.get(position);
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            contentViewHolder.mainBookName.setText(shelfItemBook.getName());
            contentViewHolder.mainBookAuthor.setText(shelfItemBook.getAuthorName());
            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickLisener != null) {
                        mOnItemClickLisener.itemClick(shelfItemBook);
                    }
                }
            });
        } else {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(new Intent(mContext, ImportLocalBookActivity.class));
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size() + 1;
    }

    public void setList(List<ShelfItemBook> shelfItemBooks) {
        this.mDatas = shelfItemBooks;
        this.notifyDataSetChanged();
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.main_add_book)
        TextView mainAddBook;

        FootViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.main_book_cove)
        ImageView mainBookCove;
        @Bind(R.id.main_book_name)
        TextView mainBookName;
        @Bind(R.id.main_book_author)
        TextView mainBookAuthor;

        ContentViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void setOnItemClickLisener(OnItemClickLisener l) {
        this.mOnItemClickLisener = l;
    }

    public interface OnItemClickLisener {
        public void itemClick(ShelfItemBook shelfItemBook);
    }
}
